//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.Monitor
{
    using System;
    using System.Windows.Controls;
    using System.Windows.Input;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using System.Diagnostics;
    using System.Windows.Media;
    using System.IO;

    /// <summary>
    /// Interaction logic for WorkflowParametersView.xaml
    /// </summary>
    public sealed partial class WorkflowParametersView : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowParametersView"/> class.
        /// </summary>
        public WorkflowParametersView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>The model.</value>
        public WorkflowParametersPresentationModel Model
        {
            get { return this.DataContext as WorkflowParametersPresentationModel; }
            set { this.DataContext = value; }
        }

        /// <summary>
        /// Fired on link click.
        /// </summary>
        /// <param name="sender">
        /// Link.
        /// </param>
        /// <param name="e">
        /// MouseButtonEventArgs.
        /// </param>
        private void OnLinkClick(object sender, MouseButtonEventArgs e)
        {
            TextBlock link = sender as TextBlock;
            if (link != null)
            {
                WorkflowOutputProperty outputProperty = ((ContentPresenter)(link.TemplatedParent)).DataContext as WorkflowOutputProperty;
                if (outputProperty != null)
                {
                    DataProductItem dataproductView = outputProperty.Data;
                    if (dataproductView != null)
                    {
                        if (dataproductView.HtmlPath.Equals("about:blank") || !System.IO.File.Exists(dataproductView.HtmlPath))
                        {
                            dataproductView = Model.RetrieveDataProduct(dataproductView.OriginalDataProduct);
                        }

                        link.Foreground = Brushes.BlueViolet;
                        try
                        {
                            Process.Start(dataproductView.HtmlPath);
                        }
                        catch (IOException ex)
                        {
                            TridentErrorHandler.HandleUIException(ex);
                        }
                        catch (System.ComponentModel.Win32Exception ex)
                        {
                            TridentErrorHandler.HandleUIException(ex);
                        }
                        catch (ObjectDisposedException ex)
                        {
                            TridentErrorHandler.HandleUIException(ex);
                        }
                        catch (Exception ex)
                        {
                            TridentErrorHandler.HandleUIException(ex);
                        }
                    }
                }
            }
        }
    }
}
